#!/usr/bin/env perl
# Time-stamp: <29 May 00 15:14:35 giron1>

=head1 NAME

filtlist.perl - filters speech files list

=head1 SYNOPSIS

 filtlist.perl [options ] I<filein> I<fileout>

see also:

L<"OPTIONS">

at the end of this document.

=head1 DESCRIPTION

This programs filters a filename list by taking into account a
list of erroneous or exclude files with the option I<-Exc>. This
list must be a comma separated one without any space characters.

=head1 AUTHOR

 $Id: filtlist.perl,v 1.1 2007/10/08 09:26:02 schweitz Exp $
 Franck Giron

=head1 LOG

 $Log: filtlist.perl,v $
 Revision 1.1  2007/10/08 09:26:02  schweitz
 version 2.4, lib vergessen

 Revision /main/7  17-Jan-2003.12:25:42  giron1
 Updating header to /usr/bin/env perl.
 
 Revision /main/6  17-Jan-2003.11:07:43  giron1
 Changing header to /usr/bin/env perl
 
 Revision 1.5  2000/05/29 14:30:48  giron1
 -Exc instead of -exc beacause extension option start with -e

 Revision 1.4  2000/04/27 10:20:12  giron1
 Pod documentation updated


=cut

#----------------------------------------------------------------------
# perl packages
#----------------------------------------------------------------------

use strict;
use OPTIONS;

#----------------------------------------------------------------------
#                       FUNCTIONS Description
#----------------------------------------------------------------------
sub excludefromlist {
  my ($listorg,$opt) = @_;


  my @excludelists = split /,/, $opt->{'Exc'}{'string'};

  my @listin = sort @$listorg;
  my @listout = ();

  # Use eventually the Exclude lists
  my $excfile;
  foreach $excfile (@excludelists) {
    if (-f "$excfile" ) {
      my $fh = new FileHandle;
      if ($fh->open("<$excfile")) {
	while (<$fh>) {
	  chop;
	  push @listout,$_ unless (/^\#/ or /^\s*$/);
	}
	$fh->close;
	($#listout >= 0) && $opt->{'echo'}{'Do'} && 
	  warn $excfile,' found:',$#listout+1," total entries.\n";
      } else {die "Error by opening $excfile\nCheck your exclude files list.";}
    }
  }

  # Exclude files from the preceding lists
  my @listkeep = ();
  if ($#listout >= 0) {
  LOOP: foreach (@listin) {
      # Check the Out list
      my $i;
      for ($i=0; $i<=$#listout; $i++) {
	if (m/$listout[$i]/) {
	  $opt->{'echo'}{'Do'} && warn "Out:$i, $_, $listout[$i]\n";
	  my $p = $_; next LOOP;}
      }
      push @listkeep , $_;
    }
    $opt->{'echo'}{'Do'} && warn 'After exclusion:',$#listkeep+1," elements\n";
  }
  else {@listkeep = @listin};

  # Look for multiple entries like a001a, a001b and takes the last one
  if ($opt->{'stc'}{'Do'}) {
    my @listfinal = ();
    my $prev = shift @listkeep;
    push @listfinal,$prev;
    foreach (@listkeep) {
      chop $prev;
      if ( m/$prev/) {
	pop @listfinal;
      }
      push @listfinal, $_;
      $prev = $_;
    }
    $opt->{'echo'}{'Do'} && 
      warn 'After taking last version:',$#listfinal+1," elements\n";   
    return @listfinal;
  }
  else {return @listkeep;}
}

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------

# Define the set of options

my $OptList = new OPTIONS;

$OptList->stc('Do' => 0, 'Usage' => 'flag for STC modus');
$OptList->echo('Do' => 0, 'Usage' => 'displays informations while running');
$OptList->Exc('string' => 'ErroneousSignals', 'Usage' => '<lists>: exclude lists');

# Command processing

$OptList->GetOpt || die "Check your options\n";

my $numargs = @ARGV;
my $usage = ($numargs ne 2);

if ($usage) {
  $OptList->Usage("filein fileout");
}

# Read the original list
my $fh = new FileHandle;
my @list= ();

$fh->open($ARGV[0],'r');
while (<$fh>) { chop; push @list, $_;}
$fh->close;

$OptList->{'echo'}{'Do'} && warn "Starting list $ARGV[0] has:",$#list+1," elements\n";

# Process it
my @listout = excludefromlist(\@list,$OptList);

$OptList->{'echo'}{'Do'} && warn "Final list $ARGV[1] has:",$#listout+1," elements\n";

# Write it
$fh->open($ARGV[1],'w');
foreach (@listout) {$fh->print("$_\n");}
$fh->close;

exit 0;

__END__

=head1 COMMANDS

The following commands are supported:

=over 14

=item -stc

for sorting multiple entries by the last character. In the case of STC
recorded files, if there are files a,b,c,... for the same utterance
number, the one with the highest letter is selected.

=item -echo

displays some information about the running process.

=back

=head1 OPTIONS

The following options are defined:

=over 14

=item -Exc I<list1,list2>

 comma separated exclude file lists 
 (default: ErroneousSignals)

=back
